package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ShoppingNavigationConverter;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dto.ShoppingNavigationDto;
import com.cskaoyan.shopping.dto.ShoppingNavigationResponse;
import com.cskaoyan.shopping.service.ShoppingNavigationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 类说明
 *
 * @author zhangfuqiang
 * @date 2022/5/18
 */
@Service
public class ShoppingNavigationServiceImpl implements ShoppingNavigationService {

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ShoppingNavigationConverter shoppingNavigationConverter;

    @Override
    public ShoppingNavigationResponse getShoppingNavigation() {
        ShoppingNavigationResponse shoppingNavigationResponse = new ShoppingNavigationResponse();
        try {
            //拿到查询数据
            Example example = new Example(PanelContent.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("panelId", 0);
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);

            //转换数据
            List<ShoppingNavigationDto> shoppingNavigationDtos = shoppingNavigationConverter.panelContentsDo2ShoppingNavigationsDto(panelContents);
            shoppingNavigationResponse.setResponse(shoppingNavigationDtos);
            shoppingNavigationResponse.setTimestamp(System.currentTimeMillis());
            shoppingNavigationResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            shoppingNavigationResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(shoppingNavigationResponse, e);
        }

        return shoppingNavigationResponse;
    }
}
